package xuli.servlet;

import entity.*;

import xuli.dao.BrandDao;
import xuli.dao.UserDao;
import xuli.dao.impl.BrandDaoImpl;
import xuli.dao.impl.UserDaoImpl;
import xuli.service.ProductHomeService;
import xuli.service.impl.ProductHomeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * @author xuli
 */
@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String product_id = request.getParameter("product_id");
        HttpSession session = request.getSession();
        String model_id = request.getParameter("model_id");
        String contextPath = request.getContextPath();
        if (product_id == null) {
            session.setAttribute("msg", "请输入商品的id");
            response.sendRedirect(contextPath + "/errorInfo.jsp");
            return;
        }
        ProductHomeService phs = new ProductHomeServiceImpl();
        Product_Info productInfo = phs.findProductById(Integer.parseInt(product_id));
        if (productInfo == null) {
            session.setAttribute("msg", "商品不存在");
            response.sendRedirect(contextPath + "/errorInfo.jsp");
            return;
        }
        if (productInfo.getWarehouse_products() == null || productInfo.getWarehouse_products().isEmpty()) {
            session.setAttribute("msg", "商品没有任何型号,无法展示");
            response.sendRedirect(contextPath + "/errorInfo.jsp");
            return;
        }
        if (productInfo.getBrand_id() == null) {
            session.setAttribute("msg", "商品没有品牌,无法展示");
            response.sendRedirect(contextPath + "/errorInfo.jsp");
            return;
        } else {
            BrandDao brandDao = new BrandDaoImpl();
            Brand_Info brandInfo = brandDao.findById(productInfo.getBrand_id());
            if (brandInfo == null) {
                session.setAttribute("msg", "商品没有品牌,无法展示");
                response.sendRedirect("/errorInfo.jsp");
                return;
            } else {
                request.setAttribute(contextPath + "brand", brandInfo);
            }
        }
        request.setAttribute("productInfo", productInfo);
        if (model_id == null || "".equals(model_id.trim())) {
            model_id = productInfo.getWarehouse_products().get(0).getModel_id();
        } else {
            boolean b = false;
            for (Warehouse_Product warehouse_product : productInfo.getWarehouse_products()) {
                if (warehouse_product.getModel_id().equals(model_id)) {
                    b = true;
                }
            }
            if (!b) {
                model_id = productInfo.getWarehouse_products().get(0).getModel_id();
            }
        }
        Warehouse_Product warehouseInfo = phs.findWarehouseById(product_id, Integer.parseInt(model_id));
        request.setAttribute("modelInfo", warehouseInfo);
        User user = (User) request.getSession().getAttribute("SywUser");
        request.setAttribute("user", user);
        request.setAttribute("formatDate", productInfo.getIndate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        List<Product_Info> recommendProducts = phs.findRandom(3);
        for (int i = 0; i < recommendProducts.size(); i++) {
            if (recommendProducts.get(i).getWarehouseProduct() == null) {
                recommendProducts.remove(i);
                i--;
            }
        }
        request.setAttribute("recommendProducts", recommendProducts);
        Random random = new Random();
        int i = random.nextInt(5);
        List<Product_Info> anotherLikeProducts = phs.findRandom(i);
        request.setAttribute("anotherLikeProducts", anotherLikeProducts);
        request.getRequestDispatcher("Product.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
